﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using He.Common;
using System.Xml;

namespace He.Web.manage.template
{
    public partial class list : He.Web.UI.Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RptBind();
            }
        }

        #region 数据绑定===============================================
        private void RptBind()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("skinname", Type.GetType("System.String"));
            dt.Columns.Add("name", Type.GetType("System.String"));
            dt.Columns.Add("img", Type.GetType("System.String"));
            dt.Columns.Add("author", Type.GetType("System.String"));
            dt.Columns.Add("createdate", Type.GetType("System.String"));
            dt.Columns.Add("version", Type.GetType("System.String"));
            dt.Columns.Add("fordntver", Type.GetType("System.String"));

            DirectoryInfo dirInfo = new DirectoryInfo(PathHelper.MapPath("../../templates/"));
            try
            {
                foreach (DirectoryInfo dir in dirInfo.GetDirectories())
                {

                    DataRow dr = dt.NewRow();
                    Model.templates model = GetInfo(dir.FullName);
                    dr["skinname"] = dir.Name;  //文件夹名称
                    dr["name"] = model.name;    // 模板名称
                    dr["img"] = "../../templates/" + dir.Name + "/about.png";   // 模板图片
                    dr["author"] = model.author;    //作者
                    dr["createdate"] = model.createdate;    //创建日期
                    dr["version"] = model.version;  //模板版本
                    dr["fordntver"] = model.fordntver;  //适用的版本
                    dt.Rows.Add(dr);
                }
            }
            catch
            {
 
            }

            this.rptList.DataSource = dt;
            this.rptList.DataBind();
        }
        #endregion

        #region 读取模板配置信息========================================
        /// <summary>
        /// 从模板说明文件中获得模板说明信息
        /// </summary>
        /// <param name="xmlPath">模板路径(不包含文件名)</param>
        /// <returns>模板说明信息</returns>
        private Model.templates GetInfo(string xmlPath)
        {
            Model.templates model = new Model.templates();
            ///存放关于信息的文件 about.xml是否存在,不存在返回空串
            if (!File.Exists(xmlPath + @"\about.xml"))
            {
                return null;
            }
            try
            {
                XmlNodeList xnList = XmlHelper.ReadNodes(xmlPath + @"\about.xml", "about");
                foreach (XmlNode n in xnList)
                {
                    if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "template")
                    {
                        model.name = n.Attributes["name"] != null ? n.Attributes["name"].Value.ToString() : "";
                        model.author = n.Attributes["author"] != null ? n.Attributes["author"].Value.ToString() : "";
                        model.createdate = n.Attributes["createdate"] != null ? n.Attributes["createdate"].Value.ToString() : "";
                        model.version = n.Attributes["version"] != null ? n.Attributes["version"].Value.ToString() : "";
                        model.fordntver = n.Attributes["fordntver"] != null ? n.Attributes["fordntver"].Value.ToString() : "";
                    }
                }
            }
            catch
            {
                return null;
            }
            return model;
        }
        #endregion

        #region 设置模版操作========================================
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name = ((Label)e.Item.FindControl("lb_id")).Text;
            BLL.site_info bll = new BLL.site_info();
            switch (e.CommandName.ToLower())
            {
                case "ibtnset":
                    if (siteinfo.Template == name)
                    {
                        ShowError("该模版当前已被启用，无需重复操作。");
                        return;
                    }
                    siteinfo.Template = name;
                    new BLL.config().SaveConfig<Model.site_info>(siteinfo, string.Format("~/config/lang_{0}.config", this.lang));
                    break;
            }
            ShowSuccess("模版启用成功啦！",Request.RawUrl);
        }
        #endregion
    }
}